package top.lethelh.jianoa.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.lethelh.jianoa.common.util.R;
import top.lethelh.jianoa.config.shiro.ThreadLocalToken;

/**
 * @author lh
 * @date 2021/11/21 10:41 上午
 * @desc
 */
@Aspect
@Component
public class TokenAspect {
    @Autowired
    private ThreadLocalToken threadLocalToken;

    @Pointcut("execution(public * top.lethelh.jianoa.controller.*.*(..))")
    public void aspect() {

    }

    @Around("aspect()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
       R r = (R) point.proceed();
       String token = threadLocalToken.getToken();
       if (token != null) {
           r.put("token", token);
           threadLocalToken.clear();
       }
       return r;
    }
}
